<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        /* 保持原样式不变 */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #1a2980 0%, #26d0ce 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            background: rgba(255, 255, 255, 0.92);
            width: 100%;
            max-width: 400px;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 15px 25px rgba(0, 0, 0, 0.2);
        }
        h2 {
            text-align: center;
            margin-bottom: 25px;
            color: #2c3e50;
            position: relative;
        }
        h2:after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 50px;
            height: 3px;
            background: #2ecc71;
        }
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #34495e;
            font-weight: 500;
        }
        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }
        input:focus {
            border-color: #2ecc71;
            box-shadow: 0 0 0 2px rgba(46, 204, 113, 0.2);
            outline: none;
        }
        .btn {
            background: #2ecc71;
            color: white;
            border: none;
            padding: 12px;
            width: 100%;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            transition: background 0.3s;
            position: relative;
        }
        .btn:hover {
            background: #27ae60;
        }
        .btn.loading::after {
            content: '';
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            width: 16px;
            height: 16px;
            border: 2px solid #fff;
            border-radius: 50%;
            border-right-color: transparent;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            0% { transform: translateY(-50%) rotate(0deg); }
            100% { transform: translateY(-50%) rotate(360deg); }
        }
        .links {
            text-align: center;
            margin-top: 20px;
        }
        .links a {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
            display: block;
            margin: 8px 0;
        }
        .links a:hover {
            text-decoration: underline;
        }
        .message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 5px;
            text-align: center;
            font-weight: 500;
            display: none;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .message.loading {
            background-color: #d1ecf1;
            color: #0c5460;
        }
    </style>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <h2>欢迎登录</h2>
    <div id="message" class="message"></div>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <button type="submit" class="btn">登录</button>
    </form>
    <div class="links">
        <a href="/register">创建新账户</a>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();
        const loginBtn = document.querySelector('.btn');
        const messageBox = document.getElementById('message');

        // 简单的前端验证
        if (!username) {
            showMessage(messageBox, '请输入用户名', 'error');
            return;
        }
        if (!password) {
            showMessage(messageBox, '请输入密码', 'error');
            return;
        }

        // 显示加载状态
        loginBtn.disabled = true;
        loginBtn.classList.add('loading');
        showMessage(messageBox, '正在验证...', 'loading');

        // 发送登录请求（携带Session信息）
        fetch('/api/auth/login', {  // 直接使用你的登录接口
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams({ username, password }),
            credentials: 'include'  // 关键：允许跨域携带Cookie（Session依赖）
        })
            .then(response => {
                // 无论成功失败都先获取响应文本
                return response.text().then(text => ({
                    status: response.status,
                    text: text
                }));
            })
            .then(data => {
                // 重置按钮状态
                loginBtn.disabled = false;
                loginBtn.classList.remove('loading');

                if (data.status === 200) {
                    // 登录成功，立即跳转到首页
                    // 这里依赖后端重定向逻辑
                    window.location.href = '/';
                } else {
                    // 登录失败（用户名不存在/密码错误等）
                    throw new Error(data.text || '登录失败，请重试');
                }
            })
            .catch(error => {
                console.error('登录错误:', error);
                showMessage(messageBox, error.message || '登录失败，请重试', 'error');
                loginBtn.disabled = false;
                loginBtn.classList.remove('loading');
            });
    });

    // 封装消息显示函数
    function showMessage(element, text, type) {
        element.textContent = text;
        element.className = 'message ' + (type || '');
        element.style.display = 'block';
    }
</script>
</body>
</html>